<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="author" content="金西振">
    <title></title>
    <style>
    body{
        margin: 0;
    }
    *{
        box-sizing: border-box;
    }
    ul{
        margin: 0;
        padding: 0;
        list-style: none;
    }
    header{
        height: 10px;
        /*background-color: lightcoral;*/
    }
    .container{
        width: 900px;
        margin: 0 auto;
    }
    .nav{
        width: 100%;
        background-color: #01204f;
    }
    .nav-list{
        position: relative;
        display: flex; 
    }
    .nav-list li{
        /* float: left;   */
        height: 40px;
        line-height: 40px;
        z-index: 100;
        /* position: relative; */
    }
    .nav-list li.first{
        background-color: red;
        width: 60px;
        text-align: center;
        margin-right: 2px;
    }
    .nav-list li a{
        color: white;
        text-decoration: none;
        display: block;
        width: 100%;
        height: 100%;
        padding: 0 10px;
    }
    .bg{
        height: 40px;
        width: 60px;
        background-color: red;
        position: absolute;
        top: 0;
        left: 0;
        transition: all 0.3s linear;
        /* z-index: -1;  */
    }

    .clearfix::after{
        content: '';
        clear: both;
        display: block;
        height: 0;
        visibility: hidden;
    }

    main{
        height: 2000px;
        background-color: lightgreen;
    }
    .back-to-top{
        width: 80px;
        height: 30px;
        background-color: purple;
        text-align: center;
        line-height: 30px;
        position: fixed;
        right: 30px;
        bottom: -30px;
        transition: all 0.5s linear;
    }
    .dinglan {
            line-height: 20px;
            width: 100%;
            height: 30px;
            border: 1px solid lightgray;
        }
        .dinglan1 {
            margin-left: 1200px;
        }
        
        .dinglan li {
            font-weight: 900;
            font-size: 15px;
            list-style-type: none;
            float: left;
            margin-right: 10px;
        }
        .sousuo {
            margin-top: 20px;
            text-align: center;
        }
        
        .sousuo img {
            height: 40px;
        }
        
        .a {
            width: 500px;
            height: 40px;
        }
        
        .b {
            width: 100px;
            height: 40px;
            background-color: #38f;
        }
        
        .sousuo span {
            color: white;
            width: 100px;
            height: 100px;
            background-color: #38f;
        }
        
        .xuanze {
            margin-left: 640px;
        }
        .input2 {
            width: 100px;
            height: 40px;
            cursor: pointer;
            background-color: #3388ff;
            border: 1px solid #3388ff;
            font-size: 15px;
            color: white;
            text-align: center;
            position: absolute;
            margin-top: 14px;
            margin-left: -100px;
        }
    </style>
</head>
<body>
    <div class="dinglan">
        <ul class="dinglan1">
            <li><a href="">网页</a></li>
            <li><a href="">新闻</a></li>
            <li><a href="">贴吧</a></li>
            <li><a href="">知道</a></li>
            <li><a href="">音乐</a></li>
            <li><a href="">图片</a></li>
            <li><a href="">视频</a></li>
            <li><a href="">地图</a></li>
            <li><a href="">文库</a></li>
            <li>|</li>
            <li><a href="">百度首页</a></li>
            <li><a href="">登录</a></li>
            <li><a href="">注册</a></li>
            <li><a href="">百度新闻客户端</a></li>
        </ul>
    </div>

    <div class="sousuo">
        <img src="./img/baidu.png" alt="">
        <input class="a" type="" value="">
        <button type="button" class="input2">百度一下</button>
        <a href="">帮助 高级搜索 设置</a>
    </div>
    <div class="xuanze">
        <label>新闻全文<input type="radio" name="新闻全文"></label>
        <label>新闻标题<input type="radio" name="新闻全文"></label>
    </div>



    <header></header>
    <nav class="nav">
        <div class="container">
            <ul class="nav-list clearfix">
                <li class="first"><a href="#">首页</a></li>
                <li><a href="#">百家号</a></li>
                <li><a href="#">国内</a></li>
                <li><a href="#">国际</a></li>
                <li><a href="#">军事</a></li>
                <li><a href="#">社会</a></li>
                <li><a href="#">科技</a></li>
                <li><a href="#">个性推荐</a></li>
                <div class="bg"></div>
            </ul>
        </div>
    </nav>
    <div class="container">
        <main>
            
        </main>
    </div>
    <div class="back-to-top">返回顶部</div>
</body>
</html>
<script>

    var lis = document.querySelectorAll('.nav-list li');
    var bg = document.querySelector('.bg');
    for(var i = 0; i < lis.length; i ++){
        // 鼠标进来 改变 bg 的位置和宽度
        lis[i].onmouseover = function(){
            // console.log(this.offsetLeft);
            // 元素距离容器左边的偏移距离
            var left = this.offsetLeft;
            // 设置bg的偏移距离
            bg.style.left = left + 'px';
            // 获取标签的宽度
            var width = this.offsetWidth;
            bg.style.width = width + 'px';
        }
        // 鼠标出去的时候，恢复bg的位置和宽度
        lis[i].onmouseout = function(){
            bg.style.left = 0;
            bg.style.width = '60px';
        }
    }

    // 点击返回顶部
    var backToTop = document.querySelector('.back-to-top');
    backToTop.onclick = function(){
       document.documentElement.scrollTop =
       document.body.scrollTop = 0;
    }
    // 动态固定位置
    var nav = document.querySelector('.nav');
    window.onscroll = function(){
        var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
        console.log(scrollTop);
        
        if (scrollTop >= 120) {
            // 固定nav
            nav.style.position = 'fixed';
            nav.style.top = '0';
        }else{
            // 去除固定位置的效果
            nav.style.position = 'static';
        }
        // 当页面卷去的大小超过300时，出现返回顶部的按钮
        // 否则，隐藏
        if (scrollTop > 300) {
            backToTop.style.bottom = '30px';
        } else {
            backToTop.style.bottom = '-30px';
        }

    }

    

    // a = b = 2;


</script>